package code1;

/**
 * @author noob
 * @version 1.0
 * @date 2021/3/16 20:23
 * 递归实现：二分查找
 */
public class BinarySearchR {

    public BinarySearchR() {}


    public static <E extends Comparable<E>> int search(E[] data,E target){
        return search(data,0,data.length-1,target);
    }

    private static <E extends Comparable<E>> int search(E[] data, int left, int right, E target) {

        if(left > right) {
            return -1;
        }
        int mid = left + (right -left)/2;

        if(data[mid].compareTo(target)==0){
            return mid;
        }else  if(data[mid].compareTo(target) < 0){
           return  search(data,mid+1,right,target);
        }
        return search(data,left,mid-1,target);
    }

}
